package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.model.exceptions.UacBizException;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-01-18 10:14
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping(value = "/api")
public class MdcApiController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @PostMapping(value = "/throwExec")
    public Wrapper throwExec(Integer result, Long groupId, String code) {

        switch (result) {
            case 0 :
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                        .getMessage("operation.success", null));
            case 1 :
                return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                        .getMessage("operation.failed", null));
            case 2 :
                throw new UacBizException(ErrorCodeEnum.UAC10012009);
            case 3 :
                throw new UacBizException(ErrorCodeEnum.UAC10013001, 1);
            case 4 :
                // 获取消息模板
                /**
                 * 根据机构Id获取语言配置，默认中文
                 *
                 * 使用语言和配置模板获取消息模块
                 */

//                redisTemplate.opsForValue().set(RedisKeyUtil.getCsunGatewayKey(dmcDevice.getDeviceNo()),
//                    csunGatewayData);

//                String msgFormat = getMsgFormat(groupId, code);
//                String msgFormat = "[%s]的[%s]使用紧急按钮进行了呼叫, 请前往处理!";
//                String message = String.format(msgFormat, "param-1", "param-2");
                Object params[] = {"param-1", "param-2"};
//                String language = uacGroupLanguageService.getLanguageByGroupId(groupId);
//                String message = cmnyMessageSource.getMessage(code, param, language);
//                log.info("language = {}", language);
//                Locale locale = Objects.nonNull(language) ? new Locale(language) : Locale.getDefault();
//                String message = messageSource.getMessage(code, params, locale);

                String message = cmnyMessageSource.getMessage(code, params, groupId);

                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, message);

        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

}
